package com.nickstumpos.android.easyVolume.volume.utils;

import android.media.AudioManager;

import com.nickstumpos.android.easyVolume.streams.types.Stream;
import com.nickstumpos.android.easyVolume.volume.levels.VolumeLevel;

public class VolumeUtils {

	public static VolumeLevel getCurrentLevel(AudioManager am, Stream stream) {
		int max = am.getStreamMaxVolume(stream.getAndroidConstant());
		final int inc = max / 5;
		final int vol = am.getStreamVolume(stream.getAndroidConstant());
		VolumeLevel ret = null;
		if (vol > max - inc) {
			ret = VolumeLevel.HIGH;
		} else if (vol > max - inc * 2) {
			ret = VolumeLevel.MED_HIGH;
		} else if (vol > max - inc * 3) {
			ret = VolumeLevel.MEDIUM;
		} else if (vol > max - inc * 4) {
			ret = VolumeLevel.MED_LOW;
		} else if (vol > max - inc * 5) {
			ret = VolumeLevel.MED_LOW;
		} else {
			if (Stream.RINGER.equals(stream)) {
				if (am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
					ret = VolumeLevel.VIBE_ONLY;
				} else {
					if (am.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ON) {
						ret = VolumeLevel.VIBE_ONLY;
					} else {
						ret = VolumeLevel.SILENT;
					}
				}
			} else if (Stream.NOTIFICATION.equals(stream)) {
				if (am.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION) == AudioManager.VIBRATE_SETTING_ON) {
					ret = VolumeLevel.VIBE_ONLY;
				} else {
					ret = VolumeLevel.SILENT;
				}
			} else {
				ret = VolumeLevel.SILENT;
			}

		}
		return ret;
	}

	/**
	 * @param max
	 * @param level
	 * @return appropriate level
	 */
	public static int getLevel(final int max, final VolumeLevel level) {
		final int inc = max / 5;
		int ret;
		switch (level) {
			case HIGH:
				ret = max;
				break;
			case MED_HIGH:
				ret = max - inc;
				break;
			case MEDIUM:
				ret = max - inc * 2;
				break;
			case MED_LOW:
				ret = max - inc * 3;
				break;
			case LOW:
				ret = max - inc * 4;
				break;
			case VIBE_ONLY:
				ret = 0;
				break;
			case SILENT:
				ret = 0;
				break;
			default:
				ret = max;
		}
		return ret;
	}
}